package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ShopItemListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.BusinessType;
import com.iexbuy.ihg.entity.GoodCategoryObject;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.entity.ShopItemListObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HuanGuoSearchFragment extends BaseFragment {

	private static HuanGuoSearchFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.categorySelectLayout)
	private LinearLayout categorySelectLayout;
	@ViewInject(R.id.categoryLayout)
	private LinearLayout categoryLayout;
	@ViewInject(R.id.categoryListView)
	private ListView categoryListView;
	@ViewInject(R.id.categoryBtn)
	private Button categoryBtn;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.adItemList)

	private List<ShopItemListObject> shopItemListObjects = new ArrayList<ShopItemListObject>();
	private int pageNo = 0;
	private ShopItemListAdapter shopItemListAdapter;

	//云购
	private static List<GoodCategoryObject> goodCategoryObjects = new ArrayList<GoodCategoryObject>();
	private static GoodCategoryObject selectGoodCategory;

	//热门分类
	private List<BusinessType> businessTypes = new ArrayList<BusinessType>();
	private List<View> businessTypePageViews = new ArrayList<View>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_huan_gou_search_result, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_huo_guo_search));

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshDate();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				loadMore();
			}
		});
		msgListRefreshLayout.autoRefresh();

		if ("-1".equalsIgnoreCase(selectGoodCategory.getId()) || "-2".equalsIgnoreCase(selectGoodCategory.getId())) {
			categoryBtn.setText("全部");
		} else {
			categoryBtn.setText(selectGoodCategory.getName());
		}

		initHotCategoryView();
	}

	//刷新整个页面数据
	private void refreshDate() {
		pageNo = 0;
		initListDate();
	}

	/**
	 * 查询列表
	 */
	private void initListDate() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("payType", "1");//1:金币兑换 2:人民币支付
		paramMap.put("goodsType", "2");//1: 代表默认商品(直接输入现金)， 2: 普通商品（输入数量）,3: 其他商品
		if (!"-1".equalsIgnoreCase(selectGoodCategory.getId()) && !"-2".equalsIgnoreCase(selectGoodCategory.getId())) {
			paramMap.put("category", selectGoodCategory.getId());
		}
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_DEFAULT_SHOP_PRODUCTS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						ShopItemListObjectResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopItemListObjectResponse.class);
						if (pageNo == 0) {
							shopItemListObjects.clear();
						}
						shopItemListObjects.addAll(rsp.getBody());
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initListView() {
		shopItemListAdapter = new ShopItemListAdapter(mBaseActivity, shopItemListObjects);
		listView.setAdapter(shopItemListAdapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				HuanGouItemDetailFragment.setShopItemListObject(shopItemListObjects.get(i));//列表无头部
				CommUtil.openSingleFragmentActivity(mBaseActivity, HuanGouItemDetailFragment.class);
			}
		});
		shopItemListAdapter.notifyDataSetChanged();
	}

	private void loadMore() {
		pageNo ++;
		initListDate();
	}

	private void initHotCategoryView() {

		categoryListView.setAdapter(new BaseAdapter() {
			@Override
			public int getCount() {
				return goodCategoryObjects.size();
			}

			@Override
			public Object getItem(int i) {
				return goodCategoryObjects.get(i);
			}

			@Override
			public long getItemId(int i) {
				return 0;
			}

			@Override
			public View getView(int i, View view, ViewGroup viewGroup) {
				if (view == null) {
					view = LayoutInflater.from(mBaseActivity).inflate(R.layout.list_item_single_text, null);
				}
				((TextView) view.findViewById(R.id.itemName)).setText(goodCategoryObjects.get(i).getName());
				return view;
			}
		});
		categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				selectGoodCategory = goodCategoryObjects.get(i);
				categoryBtn.setText(selectGoodCategory.getName());
				categoryLayout.setVisibility(View.GONE);
				initListDate();
			}
		});
	}

	@OnClick(R.id.categorySelectLayout)
	public void onCategoryClick(View view) {
		if (categoryLayout.isShown()){
			categoryLayout.setVisibility(View.GONE);
		}else{
			categoryLayout.setVisibility(View.VISIBLE);
		}

	}
	@OnClick(R.id.categoryLayoutMask)
	public void categoryLayoutMaskClick(View view) {
		categoryLayout.setVisibility(View.GONE);
	}

	public static void setGoodCategoryObjects(List<GoodCategoryObject> goodCategoryObjects) {
		HuanGuoSearchFragment.goodCategoryObjects = goodCategoryObjects;
	}

	public static void setSelectGoodCategory(GoodCategoryObject selectGoodCategory) {
		HuanGuoSearchFragment.selectGoodCategory = selectGoodCategory;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static HuanGuoSearchFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
